<?php
/**
 * sys page
 * @package kernel_lib_sys
 */
load_lib('/sys/component');
/**
 * sys page
 * 
 * @author pandajingjing
 * @package kernel_lib_sys
 */
abstract class sys_page extends sys_component{

	/**
	 * 入口方法（调度使用）
	 */
	function doView(){
		$this->doPage();
	}

	/**
	 * 获取当前页面所有的样式
	 * 
	 * @return array
	 */
	protected function useStyles(){
		return $this->_useResource('style');
	}

	/**
	 * 获取当前页面所有的JS
	 * 
	 * @return array
	 */
	protected function useScripts(){
		return $this->_useResource('script');
	}

	/**
	 * 使用资源
	 * 
	 * @param $p_strType string       	
	 * @return array
	 */
	private function _useResource($p_strType){
		$arrOResource=$arrSResource=$arrCBResource=array();
		if('script'==$p_strType){
			$strExt='.js';
			$arrPageSResource=$this->useSScript();
			$arrPageOResource=$this->useOScript();
			$arrPageCBResource=$this->useCBScript();
			$arrInlineResource=$this->getLScript();
			$strSMethod='useSScript';
			$strCBMethod='useCBScript';
			$strOMethod='useOScript';
		}else{
			$strExt='.css';
			$arrPageSResource=$this->useSStyle();
			$arrPageOResource=$this->useOScript();
			$arrPageCBResource=$this->useCBStyle();
			$arrInlineResource=$this->getLStyle();
			$strSMethod='useSStyle';
			$strCBMethod='useCBStyle';
			$strOMethod='useOStyle';
		}
		$bolCombin=get_config('combination','resource');
		$arrComponent=$this->useComponent();
		$arrComponentSResource=$arrComponentOResource=$arrComponentCBResource=array();
		foreach($arrComponent as $strComponent){
			$this->loadComponentClass($strComponent);
			$strComponentName=path_to_componentname($strComponent);
			$objRelClass=new ReflectionClass($strComponentName);
			$objRelMethod=$objRelClass->getMethod($strSMethod);
			$arrComponentSResource=array_merge($arrComponentSResource,$objRelMethod->invoke(null));
			$objRelMethod=$objRelClass->getMethod($strOMethod);
			$arrComponentOResource=array_merge($arrComponentOResource,$objRelMethod->invoke(null));
			if(!$bolCombin){
				$objRelMethod=$objRelClass->getMethod($strCBMethod);
				$arrComponentCBResource=array_merge($arrComponentCBResource,$objRelMethod->invoke(null));
			}
		}
		if($bolCombin){
			$arrPageCBResource=array( 
					pagename_to_path(get_class($this)).$strExt 
			);
		}
		$arrPageSResource=array_map(function($strResource){
			return '/page'.$strResource;
		},$arrPageSResource);
		$arrPageCBResource=array_map(function ($strResource){
			return '/page'.$strResource;
		},$arrPageCBResource);
		
		$arrComponentSResource=array_map(function ($strResource){
			return '/component'.$strResource;
		},$arrComponentSResource);
		$arrComponentCBResource=array_map(function ($strResource){
			return '/component'.$strResource;
		},$arrComponentCBResource);
		$arrSResource=array_merge($arrPageSResource,$arrComponentSResource);
		$arrCBResource=array_merge($arrComponentCBResource,$arrPageCBResource);
		$arrOResource=array_merge($arrPageOResource,$arrComponentOResource);
		return array( 
				'S'=>$arrSResource,
				'CB'=>$arrCBResource,
				'O'=>$arrOResource,
				'L'=>$arrInlineResource 
		);
	}

	/**
	 * 使用的组件
	 * 
	 * @return array
	 */
	static function useComponent(){
		return array();
	}

	/**
	 * 渲染页面
	 */
	protected function doPage(){
		$this->loadPageUI(dirname(pagename_to_path(get_class($this))).'/'.$this->getView());
	}

	/**
	 * 加载页面
	 * 
	 * @param $p_strPath string       	
	 */
	protected function loadPageUI($p_strPath){
		$this->loadUI($p_strPath,'page');
	}
}